package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baidu.ueditor.ActionEnter;
import com.sl.au.entity.BussinessContent;
import com.sl.au.entity.BussinessInspection;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.Division;
import com.sl.au.entity.User;
import com.sl.au.service.BussinessContentService;
import com.sl.au.service.BussinessLevelService;
import com.sl.au.service.DivisionService;
import com.sl.utils.MsgHelper;


/**
 * 百度编辑器Controller
 * 
 * @author yangdi
 *
 */

@RestController
@RequestMapping(value = "/ui/content")
public class ContentController extends BaseController {

	@Autowired
	BussinessContentService bussinessContentService;
	
	@Autowired
	DivisionService divisionService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("content/contentindex");
		return modelAndView;
	}
	
	/**
	 * 显示区域结构
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		//List<BussinessLevel> node=null;
		List<Division> node = null;
		
		//node=bussinessLevelService.getAllNodes();
		node = divisionService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		
		nodeMap=divisionService.getNodeMap(node);
		
		map.put("code", 1);
		map.put("nodes", nodeMap);
	
		return map;
	}
	
	@RequestMapping(value="/setContent",method = RequestMethod.POST)
	public MsgHelper setContent(@RequestBody Map<String,Object> map){
		MsgHelper msgHelper = null;
		String divisionId = (String) map.get("divisionId");
		String content = (String) map.get("content");
		Division division = divisionService.getRepository().findOne(divisionId);
		if(division != null){
			BussinessContent bussinessContent = new BussinessContent();
			BussinessContent bussinessContentOld = bussinessContentService.getRepository().findByDivision(division);
			if (bussinessContentOld != null ) {
				bussinessContentOld.setBussinessContent(content);
				bussinessContentService.saveBussinessContent(bussinessContentOld);
			}else{
				bussinessContent.setDivision(division);
				bussinessContent.setBussinessContent(content);
				bussinessContentService.saveBussinessContent(bussinessContent);
			}
			msgHelper = MsgHelper.success();
			msgHelper.setMsg("区域层级描述保存成功！");
		}else{
			msgHelper = MsgHelper.failed();
			msgHelper.setMsg("区域层级描述保存失败！");
		}
		return msgHelper;
	}
	
	@RequestMapping("/getContent/{id}")
	public MsgHelper getContent(@PathVariable("id") String id) {
		MsgHelper msgHelper = null;
		Division division = divisionService.getRepository().findOne(id);
		BussinessContent bussinessContent = bussinessContentService.getRepository().findByDivision(division);
		if(bussinessContent != null){
			msgHelper = MsgHelper.success();
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("bussinessContent", bussinessContent);
			msgHelper.setData(map);
		}else {
			msgHelper = MsgHelper.failed();
		}
		return msgHelper;
	}
}
